package com.icode.gulimall.order.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: my_code
 * @Email: my_code@foxmail.com
 * @Create: 2021-02-05 12:48
 */
@Configuration
public class MyRabbitMQConfig {

    @Bean
    public Exchange orderEventExchange() {
        /**
         * name:    交换机名字
         * durable: 持久化的交换机
         * autoDelete: 自动删除
         */
        return ExchangeBuilder.topicExchange("order-event-exchange").build();
    }

    @Bean
    public Queue releaseQueue() {
        //默认参数 this(name, true, false, false);
        return QueueBuilder.durable("order.release.queue").build();
    }

    @Bean
    public Queue createQueue() {

        return QueueBuilder.durable("order.create.queue").deadLetterExchange("order-event-exchange")
                .deadLetterRoutingKey("order.release")
                .ttl(60000)// 消息过期时间 1分钟
                .build();

    }

    @Bean
    public Queue otherQueue(){

        return QueueBuilder.durable("order.other.queue").build();
    }

    @Bean
    public Binding orderCreateBinding() {

        return BindingBuilder.bind(createQueue()).to(orderEventExchange())
                .with("order.create").noargs();


    }

    @Bean
    public Binding orderReleaseBinding() {

        return BindingBuilder.bind(releaseQueue()).to(orderEventExchange())
                .with("order.release").noargs();
    }

    @Bean
    public Binding orderOtherBinding(){

        return new Binding("stock.release.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other",
                null);
    }

}
